import cn.stylefeng.guns.sys.core.util.Jdom2XMLUtil;
import cn.stylefeng.guns.sys.core.util.SoapUtil;
import com.alibaba.druid.util.StringUtils;
import org.dom4j.io.SAXReader;
import org.jdom2.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
public class SoapTest {
    private static Logger logger = LoggerFactory.getLogger(SoapTest.class);
    public static void main(String[] args) {
        StringBuilder soap=new StringBuilder(); //构造请求报文
        //soap.append(" <?xml version='1.0' encoding='utf-8'?>");
        soap.append(" <soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>");
        //<--Header标头信息start-->
        soap.append(" <soap:Body>");
        soap.append(" <postesalescreate xmlns='http://tempurl.org'>");
        soap.append(" <astr_request>");
        soap.append(" <header>");
        soap.append(" <licensekey></licensekey>");
        //用户名
        soap.append(" <username>SJZMIXC</username>");
        //密码
        soap.append(" <password>SJZmixc@01</password>");
        soap.append(" <lang></lang>");
        soap.append(" <pagerecords>1</pagerecords>");
        soap.append(" <pageno>1</pageno>");
        soap.append(" <updatecount>1</updatecount>");
        //消息类型
        soap.append(" <messagetype>SALESDATA</messagetype>");
        //消息id
        soap.append(" <messageid>332</messageid>");
        //版本编号
        soap.append(" <version>V332M</version>");
        soap.append(" </header>");
        //<--Header标头信息end-->

        //<--SalesTotal:销售单主表start-->
        soap.append("  <esalestotal>");
        //交易日期
        soap.append(" <txdate_yyyymmdd>20200910</txdate_yyyymmdd>");
        //交易时间
        soap.append(" <txtime_hhmmss>170723</txtime_hhmmss>");
        //商城编号
        soap.append(" <mallid>HB</mallid>");
        //店铺号
        soap.append(" <storecode>BLGL001N01</storecode>");
        //收银机号
        soap.append(" <tillid>01</tillid>");
        //销售单号
        soap.append(" <txdocno>10000791</txdocno>");
        //收银员编号
        soap.append(" <cashier>01</cashier>");
        //VIP号
        soap.append(" <vipcode></vipcode>");
        //销售员
        soap.append(" <salesman></salesman>");
        //数量
        soap.append(" <netqty>1</netqty>");
        //销售净金额
        soap.append(" <netamount>-0.99</netamount>");
        //预留字段
        soap.append(" <extendparam></extendparam>");
        soap.append(" </esalestotal>");
        //<--SalesTotal:销售单主表end-->

        //<--SalesI销售单货品明细表start-->
        soap.append(" <esalesitems>");
        soap.append(" <esalesitem>");
        //商品行号
        soap.append(" <lineno>2</lineno>");
        //商品编号
        soap.append(" <itemcode>L001N01001</itemcode>");
        //数量
        soap.append(" <qty>1</qty>");
        //折扣金额
        soap.append(" <discountamount>0</discountamount>");
        //净金额
        soap.append(" <netamount>-0.99</netamount>");
        //获得的积分
        soap.append(" <bonusearn>0</bonusearn>");
        //预留字段
        soap.append(" <extendparam></extendparam>");
        //货品备注
        soap.append(" <salesitemremark></salesitemremark>");
        soap.append(" </esalesitem>");
        soap.append(" </esalesitems>");
        //<--SalesI销售单货品明细表end-->

        //<!--销售单付款明细表start-->
        soap.append(" <esalestenders>");
        soap.append(" <esalestender>");
        //行号
        soap.append(" <lineno>2</lineno>");
        //付款代码
        soap.append(" <tendercode>CH</tendercode>");
        //付款金额
        soap.append(" <payamount>-0.99</payamount>");
        //本位币金额
        soap.append(" <baseamount>-0.99</baseamount>");
        //找零金额
        soap.append(" <excessamount>0</excessamount>");
        //预留字段
        soap.append(" <extendparam></extendparam>");
        //备注信息
        soap.append(" <remark></remark>");
        soap.append(" </esalestender>");
        soap.append(" </esalestenders>");
        //<!--SalesTender销售单付款明细表end-->

        soap.append(" </astr_request>");
        soap.append(" </postesalescreate>");
        soap.append(" </soap:Body>");
        soap.append(" </soap:Envelope>");
        String requestSoap=soap.toString();
        String serviceAddress="http://124.239.142.218:60001/eSales/esales.asmx?WSDL";//服务地址(将url替换成自己项目的地址)
        String charSet="utf-8";
        String contentType="text/xml; charset=utf-8";
        //String contentType="application/soap+xml; charset=utf-8";
        //第一步：调用方法getResponseSoap。返回响应报文和状态码
        Map<String,Object> responseSoapMap= SoapUtil.responseSoap(requestSoap, serviceAddress, charSet, contentType);
        Integer statusCode=(Integer)responseSoapMap.get("statusCode");
        if(statusCode==200){
            String responseSoap=(String)responseSoapMap.get("responseSoap");
            System.out.println("返回的报文为----"+responseSoap);
            logger.info("===返回的报文为==="+requestSoap);
           // String targetNodeName="ESB_RETURN_CODE";
            String targetNodeName="responsecode";
            //第二步：调用strXmlToDocument方法。将字符串类型的XML的响应报文 转化成Document结构文档
            Document doc=Jdom2XMLUtil.strXmlToDocument(responseSoap);
            //第三步：调用getValueByElementName方法。递归获得目标节点的值
            String result= Jdom2XMLUtil.getValueByElementName(doc,targetNodeName);
            if(!StringUtils.isEmpty(result)){
                System.out.println(result);
                logger.info("===result==="+result);
            }else{
                System.out.println("没有此节点或者没有值!");
            }
        }else{
            System.out.println("请求失败!");
        }
    }
}
